var myMenuAttr;
var myMenuModel;

myMenuAttr = {omitDefaultItems: true};
myMenuModel = {
visible: true,
items: [
	{label: "Prefences...", command: 'do-prefs'},
	{label: "Clear Cookie..", command: 'remove-cookie'},
        {label: "Clear Running..", command: 'remove-running'}
]
};

var teaMenuAttr = { items: [{visable: false}, { items: []},  {visable: false}] };
teaMenuAttr.items[1].items.push({label: "Saved", command: 'go-tea-picker'});
teaMenuAttr.items[1].items.push({label: "Add", command: 'go-add-minder'});

var teaMenuAttrRunning = { items: [{visable: false}, { items: []},  {visable: false}] };
teaMenuAttrRunning.items[1].items.push({label: "Saved", command: 'go-tea-picker'});
teaMenuAttrRunning.items[1].items.push({label: "Add", command: 'go-add-minder'});
teaMenuAttrRunning.items[1].items.push({label: "Running", command: 'go-tea-minder'});


function AppAssistant (appController) {
}

var MainStageName = "userStage";

AppAssistant.prototype.handleLaunch = function (launchParams) {

    if(!launchParams)
    {
        // Look for an existing main stage by name.
        var stageProxy = this.controller.getStageProxy(MainStageName);
        var stageController = this.controller.getStageController(MainStageName);

    	if (stageProxy) {
    	     // If the stage exists, just bring it to the front by focusing its window
    	     //   or if it's just the proxy then it's being focused, so exit
    	     if (stageController)    {
    	            stageController.window.focus();
             }
    	} else {
	// Create a callback function to set up the new main stage
	// once it is done loading. It is passed the new stage controller
	// as the first parameter.
	var pushMainScene = function(stageController) {
		stageController.pushScene('tea-picker');
            };
        var stageArguments = { name: MainStageName};

        this.controller.createStageWithCallback(stageArguments, pushMainScene, "card");
        }
    }
    else
    {
        Mojo.Log.info('start setTimeout');
         var appController = Mojo.Controller.getAppController();
        Mojo.Log.info('got app controller');
        var f = function(stageController){
            //We can't use our showScene function from our app's stage assistant here since this
            //stageController is actually a new stage controller specifically for our popup alert.
                    stageController.pushScene({name: "tea-popup"}, {message: "Your tea is ready!!", stage: 'com.fuzzymole.teaminder.popup'});
                    
            };
        Mojo.Log.info('crateing popup');
    
        appController.createStageWithCallback({
            name: "com.fuzzymole.teaminder.popup",
            lightweight: true,
            height: 125,
            soundclass: "alarm",
            sound: "/media/internal/ringtones/Dulcimer.mp3"
            }, f, 'popupalert');
    }
};

AppAssistant.prototype.handleCommand = function(event) {
    var stageController = this.controller.getStageController(MainStageName);

    if(stageController && event.type == Mojo.Event.command) {
        var currentScene = stageController.activeScene();

        switch(event.command) {
            case 'go-tea-picker':
                stageController.swapScene("tea-picker");
                break;
            case 'go-add-minder':
                stageController.swapScene("add-minder");
                break;
            case 'go-tea-minder':
                stageController.swapScene("tea-minder");
                break;
            case 'do-prefs':
                stageController.pushScene("preferences");
                break;
            case 'remove-cookie':
                Mojo.Log.info('starting remove of cookie');
                var cookie = new Mojo.Model.Cookie('TeaList');
                cookie.remove();
                Mojo.Log.info('end remove of cookie');
                stageController.swapScene("tea-picker");
                break;
            case 'remove-running':
                Mojo.Log.info('staring remove of running');
                var cookie = new Mojo.Model.Cookie('Running');
                cookie.remove();
                Mojo.Log.info('end remove of running');
                stageController.swqScene("tea-picker");
                break;
        }
    }
};